package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.QiniuUtils;
import com.ithema.service.CheckGroupService;
import com.ithema.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;


import java.io.IOException;
import java.util.UUID;

/**
 * 体验套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
   //使用jedisPool操作reids服务
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService setmealService;


    //文件上传
@RequestMapping("/upload")
  public Result upload(@RequestParam("imgFile") MultipartFile imgFile){


    String originalFilename = imgFile.getOriginalFilename(); //获取原始文件名
    int index = originalFilename.lastIndexOf(".");//截取最后.的位置索引
//    String substring = originalFilename.substring(index-1);//得到.jpg
    String substring = originalFilename.substring(index);//得到.jpg
    String fileName= UUID.randomUUID().toString()+substring; //随机生成图片名字

    try {
        //imFile.getBytes()绝对路径 ,fileName名称
        QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
       //将图片上传到七牛云的同时.往Redis里也存一份
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);

    } catch (IOException e) {
        e.printStackTrace();
        return  new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    return  new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

  }
@RequestMapping("/add")
    public  Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
    try {
        setmealService.add(setmeal,checkgroupIds);
        return  new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
    }

}

//    检查项分页查询//

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = setmealService.pageQuery(queryPageBean);
        return pageResult;
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {

        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);

        }

    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        try {
            setmealService.edit(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);

        }

    }
}
